//
//  STBaseViewController.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/9/28.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STBaseViewController.h"

@interface STBaseViewController ()

@end

@implementation STBaseViewController

- (instancetype)init
{
    NSString* nibFile = [[NSBundle mainBundle] pathForResource:NSStringFromClass([self class]) ofType:@"nib"];
    if(nibFile)
        self = [super initWithNibName:NSStringFromClass([self class]) bundle:[NSBundle mainBundle]];
    else
        self = [super init];
    
    if(self) {
        
    }
    
    return self;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    return ;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    return ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavConfig];
    
    self.view.autoresizesSubviews = NO;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    return ;
}


-(void)setNavConfig{
    
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    //[[UINavigationBar appearance] setShadowImage:[UIImage imageWithColor:UIColorFromRGB(0x1D2125)]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:19.0f], NSForegroundColorAttributeName : [UIColor blackColor]}];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    self.edgesForExtendedLayout = UIRectEdgeNone; // iOS7及以后的版本支持，设置 UIRectEdgeNone，则 self.view.frame.origin.y会下移64像素至navigationBar下方。
    
    UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] init];
    backBarBtn.title = @"返回";
    self.navigationItem.backBarButtonItem = backBarBtn;
    
    //    if (self.navigationController.viewControllers.count>1) {
    //
    ////        UIButton * backBtn = [UIButton initBackBtn];
    ////        backBtn.titleLabel.font = FONT(19);
    ////        ButtonAddTarget(backBtn, gotoBack)
    ////        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    //    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    NSLog(@"%@ dealloc",self);
    
}


-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
    
    return ;
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    return ;
}

#pragma mark -

-(UIButton*)addRightTitleItem:(NSString*)title sendBlock:(BaseVCBlock)sendBlock{
    self.baseVCBlock = sendBlock;
    UIButton * rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-15-80, 24, 80, 40)];
    rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightBtn setTitleColor:BlackColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = FONT(14);
    [rightBtn setTitle:LocalizedString(title, nil)forState:UIControlStateNormal];
    ButtonAddTarget(rightBtn, clicRightItem)
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    return rightBtn;
}
-(void)clicRightItem{
    if (self.baseVCBlock) {
        self.baseVCBlock(nil);
    }
}
-(void)addLeftItem:(UIBarButtonItem *)barButtonItem complete:(void (^)(UIBarButtonItem *))complete{
    self.navigationItem.leftBarButtonItem = barButtonItem;
    [barButtonItem setActionBlock:complete];
}

-(void)addRightItem:(UIBarButtonItem *)barButtonItem complete:(void (^)(UIBarButtonItem *))complete{
    self.navigationItem.rightBarButtonItem = barButtonItem;
    [barButtonItem setActionBlock:complete];
}

-(void)addSubview:(UIView *)subview insets:(UIEdgeInsets)insets{
    [self.view addSubview:subview];
    
    [subview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(insets);
    }];
}

-(void)keyBoardWillShowNotification:(NSNotification*)notification {
    if([self respondsToSelector:@selector(keyboardWillShow:)])
        [self keyboardWillShow:notification.userInfo];
    return ;
}

-(void)keyBoardWillHideNotification:(NSNotification*)notification {
    if([self respondsToSelector:@selector(keyboardWillHide:)])
        [self keyboardWillHide:notification.userInfo];
    return ;
}

#pragma mark -alert
-(void)showAlertWithTitle:(NSString *)title message:(NSString *)message actions:(NSArray *)actions complete:(void (^)(NSInteger))complete{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    for (NSString *actionTitle in actions) {
        NSInteger index = [actions indexOfObject:actionTitle];
        UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if(complete){
                complete(index);
            }
        }];
        
        [alert addAction:action];
    }
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark -actionSheet
-(void)showActionSheetWithTitle:(NSString *)title message:(NSString *)message actions:(NSArray *)actions complete:(void (^)(NSInteger))complete{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    for (NSString *actionTitle in actions) {
        NSInteger index = [actions indexOfObject:actionTitle];
        UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if(complete){
                complete(index);
            }
        }];
        
        [alert addAction:action];
    }
    
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)gotoBack {
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    return ;
}


-(void)keyboardWillShow:(NSDictionary *)keyboardFrameInfo {
    return ;
}

-(void)keyboardWillHide:(NSDictionary *)keyboardFrameInfo {
    return ;
}
-(NSMutableArray *)baseDataArray{
    if (!_baseDataArray) {
        _baseDataArray = [[NSMutableArray alloc] init];
    }
    return _baseDataArray;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

@end
